package bookstore.repository.impl;

import bookstore.model.BookReview;
import bookstore.repository.BookReviewDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Repository;

import java.util.Optional;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

@Repository
public class BookReviewDaoImpl implements BookReviewDao {
    @Autowired
    private MongoOperations mongoTemplate;

    @Override
    public Optional<BookReview> findById(String id) {
        return Optional.ofNullable(mongoTemplate.findOne(query(where("isbn").is(id)), BookReview.class));
    }

    @Override
    public void save(BookReview bookReview) {
        mongoTemplate.save(bookReview);
    }
}
